package com.internetCafes.spms.common.utils;

import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExportExcel {
	/**
	 * 根据标题和数据集合导出excel
	 * @param objectList : 数据集合
	 * @param titles : 标题 
	 * @param notitles : 不需要展示的属性 
	 * @param fileName : 文件名
	 * @param response : response对象
	 */
	public static void exportsExcel(List objectList, String[] titles, Map<String, Object> notitles, String fileName, HttpServletResponse response ){
		 try {
			OutputStream os = response.getOutputStream();
			response.reset();// 清空输出流        
			response.setHeader("Content-disposition", "attachment; filename="+ new String(fileName.getBytes("GB2312"),"ISO8859-1"));  
			// 设定输出文件头        
			response.setContentType("application/msexcel");// 定义输出类型      
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFSheet sheet = wb.createSheet(fileName);
			HSSFCellStyle style = wb.createCellStyle();  
		    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
		    style.setWrapText(true);
		    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直
		    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平
		    //创建标题行
		    HSSFRow row = sheet.createRow((int) 0);  
		    //将标题填入
		    for(int i=0;i<titles.length;i++){
				 HSSFCell cell = row.createCell(i);  
		         cell.setCellValue(titles[i]);  
		         cell.setCellStyle(style);
		         sheet.setColumnWidth(i, 300*100); 
		         sheet.autoSizeColumn(i);
		    }
		
		    //将数据填入
		    for(int i=0;i<objectList.size();i++){
		    	Object object = objectList.get(i);
		    	Class objectClass = object.getClass();
		    	Field[] fields = objectClass.getDeclaredFields();
		    	HSSFRow rows = sheet.createRow(i+1);
		    	int rowindex = -1;
				for (int j = 0; j < fields.length; j++) {
					if (fields[j].getName().equals("serialVersionUID")) {
						continue;
					}
					if(!fileName.equals("试题信息表.xls")){
						if (null!=notitles && null!=notitles.get(fields[j].getName())) {
							continue;
						}
					}
					rowindex++;
					String daXie = getDaXie(fields[j].getName());
					Method get = objectClass.getMethod("get" + daXie, new Class[] {});
					Object value = get.invoke(object, new Object[] {});
					HSSFCell cell = rows.createCell(rowindex);
					String values = "";
					if(value==null){
						 values = "-";
					}else{										
					 values = value.toString();
					}
					cell.setCellValue(values);
					cell.setCellStyle(style);
					if(values.getBytes("GB2312").length>titles[rowindex].getBytes("GB2312").length){
						sheet.autoSizeColumn(rowindex);
					}
					
				}
		    }
		    wb.write(os);
		    wb.close();
		    
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}// 取得输出流
		   
	}
	/**
	 * 将首字母转成大写并返回
	 */
	public static String getDaXie(String name) {
		return name.replaceFirst(name.substring(0, 1), name.substring(0, 1)
				.toUpperCase());
	}

}
